package com.lion.tag.tab;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import com.lion.tag.util.AppendUtil;

public class TabItem extends Tab {
	private static final long serialVersionUID = 822414796357935744L;
	private String title;
	private String icon;
	private String closable;
	private String href;// 异步地址
	private String tools;// 标签工具控件id编号

	/**
	 * 实现选项卡子项
	 */
	@Override
	public int doStartTag() throws JspException {
		StringBuilder sb = new StringBuilder(200);
		sb.append("<div ");
		sb = AppendUtil.addAttribute(sb, "id", id);
		sb = AppendUtil.addAttribute(sb, "title", title);
		sb = AppendUtil.addAttribute(sb, "style", style);
		sb.append("data-options=\"");
		int hasData = 0;
		if (icon != null && !icon.equals("")) {
			sb.append("iconCls:'");
			sb.append(icon);
			sb.append("'");
			hasData = 1;
		}
		if (closable != null && !closable.equals("")) {
			if (hasData == 1) {
				sb.append(",");
			}
			sb.append("closable:");
			sb.append(closable);
			hasData = 1;
		}
		if (href != null && !href.equals("")) {
			if (hasData == 1) {
				sb.append(",");
			}
			sb.append("href:'");
			if(!href.startsWith("http")){
				sb.append(getContextPath() + href);
			}else{
				sb.append(href);
			}
			sb.append("'");
			hasData = 1;
		}
		if (tools != null && !tools.equals("")) {
			if (hasData == 1) {
				sb.append(",");
			}
			if (tools.charAt(0) != '#') {
				tools = "#" + tools;
			}
			sb.append("tools:'");
			sb.append(tools);
			sb.append("'");
			hasData = 1;
		}
		sb.append("\" >");

		try {
			pageContext.getOut().write(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;// 表示标签之间的内容被正常执行
	}

	@Override
	/**  
	 *  doEndTag：当JSP容器遇到自定义标签的结束标志，就会调用doEndTag()方法。doEndTag()方法也返回一个整数值，用来决定程序后续流程。  
	 *  A.Tag.SKIP_PAGE：表示立刻停止执行网页，网页上未处理的静态内容和JSP程序均被忽略任何已有的输出内容立刻返回到客户的浏览器上。  
	 *  B.Tag.EVAL_PAGE：表示按照正常的流程继续执行JSP网页  
	 */
	public int doEndTag() throws JspException {
		try {
			pageContext.getOut().write("</div>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the icon
	 */
	public String getIcon() {
		return icon;
	}

	/**
	 * @param icon
	 *            the icon to set
	 */
	public void setIcon(String icon) {
		this.icon = icon;
	}

	/**
	 * @return the closable
	 */
	public String getClosable() {
		return closable;
	}

	/**
	 * @return the href
	 */
	public String getHref() {
		return href;
	}

	/**
	 * @param href
	 *            the href to set
	 */
	public void setHref(String href) {
		this.href = href;
	}

	/**
	 * @return the tools
	 */
	@Override
	public String getTools() {
		return tools;
	}

	/**
	 * @param tools
	 *            the tools to set
	 */
	@Override
	public void setTools(String tools) {
		this.tools = tools;
	}

	/**
	 * @param closable
	 *            the closable to set
	 */
	public void setClosable(String closable) {
		this.closable = closable;
	}

}
